package com.chenglu.android.cart.adapter;

import android.text.TextUtils;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.chenglu.android.R;
import com.chenglu.android.cart.CartFragment;
import com.chenglu.android.cart.bean.CartBean;
import com.chenglu.android.common.utils.MoneyUtils;


import java.util.List;

/**
 * @author: lyj
 * @Created: by Copyright 2023/3/29 Guangzhou Hongma Technology Co., Ltd. All rights reserved.
 * @Description：购物车布局
 */
public class CartItemAdapter extends BaseQuickAdapter<CartBean.Data.ShoppingTrolleyVOList, BaseViewHolder> {
    CartFragment cartFragment;

    public CartItemAdapter(CartFragment context, int layoutResId, @Nullable List<CartBean.Data.ShoppingTrolleyVOList> data) {
        super(layoutResId, data);
        this.cartFragment = context;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder helper, CartBean.Data.ShoppingTrolleyVOList item) {
        helper.setText(R.id.tv_cart_goodsName, item.getGoodsName());
        ImageView imageView = helper.getView(R.id.iv_cart_goodsImg);
        Glide.with(cartFragment).load(item.getGoodsImg()).into(imageView);
        if(!TextUtils.isEmpty(item.getGoodsSkuSpec())){
            helper.setText(R.id.tv_cart_goodsSpec, item.getGoodsSkuSpec())
            .setGone(R.id.tv_cart_goodsSpec,true);
        }
        helper.setText(R.id.tv_cart_goodsTotalPrice, "¥" + MoneyUtils.Algorithm.multiply(item.getGoodsPrice()+"","1",2));


        helper.setText(R.id.tv_cart_goodsNum, "" + item.getGoodsAmount());

        if (item.getGoodsAmount()>1){//数量加减显示
            helper.setBackgroundRes(R.id.iv_cart_minus,R.drawable.input_minus_default);
            helper.setEnabled(R.id.rl_cart_minus,true);
        }else {
            helper.setBackgroundRes(R.id.iv_cart_minus,R.drawable.input_minus_disabled);
            helper.setEnabled(R.id.rl_cart_minus,false);
        }
        if (item.getGoodsAmount()==item.getInventory()){
            helper.setBackgroundRes(R.id.iv_cart_add,R.drawable.input_add_disabled);
            helper.setEnabled(R.id.rl_cart_add,false);
        }else {

            helper.setBackgroundRes(R.id.iv_cart_add,R.drawable.input_add_default);
            helper.setEnabled(R.id.rl_cart_add,true);
        }

        if (item.getInventory()==0) {//缺货显示
            helper.setVisible(R.id.ll_cart_addorminus, true);
            helper.setVisible(R.id.iv_without, true);
            helper.setBackgroundRes(R.id.iv_cart_minus,R.drawable.input_minus_disabled);
            helper.setEnabled(R.id.rl_cart_minus,false);
            helper.setBackgroundRes(R.id.iv_cart_add,R.drawable.input_add_disabled);
            helper.setEnabled(R.id.rl_cart_add,false);
        } else {
            helper.setVisible(R.id.ll_cart_addorminus, true);
            helper.setVisible(R.id.iv_without, false);
        }

        helper.setChecked(R.id.cb_cart_seller_item,item.isSellerItemCheck());


        helper.setVisible(R.id.ll_cart_gift, false);//赠品不显示
//        //点击事件
        helper.addOnClickListener(R.id.rl_cart_minus);
        helper.addOnClickListener(R.id.rl_cart_add);
        helper.addOnClickListener(R.id.tv_cart_goodsName);
        helper.addOnClickListener(R.id.rv_cart_goosImg);
        helper.addOnClickListener(R.id.rl_check);
        helper.addOnClickListener(R.id.cb_cart_seller_item);
        helper.addOnClickListener(R.id.tv_delete);


    }
}
